function HandleVisas(gui, user, date, options, message) {
  this._gui = gui;
  this._user = user;
  this._date = date;
  this._options = options;
  this._message = message;
  
  this._search = (this._search) ? this._search : MClientUtils.searchVisa();
  this._page = 1;
  this._order = 't.visa_appl_date_submit';
  this._queue = 'ASC';
  
  this._createElements();
};

HandleVisas.prototype._createElements = function() {
  MClientUtils.countIt(new RequestUtils());
  
  DOMUtils.removeChildElements(this._gui.page);
  DOMUtils.removeTableRows(this._gui.result, 1);
  DOMUtils.removeChildElements(this._gui.search_nationality);
  DOMUtils.removeChildElements(this._gui.search_school);
  DOMUtils.removeChildElements(this._gui.search_staff);
  DOMUtils.removeChildElements(this._gui.search_agent);
  DOMUtils.removeChildElements(this._gui.search_visa_type);
  //Preset
  var nationalities = (this._message['nationality']) ? this._message['nationality'].data : [];
  this._gui.search_nationality.appendChild(new Option(''));
  for (var i = 0, il = nationalities.length; i < il; i++) {
    var tmp = nationalities[i];
    this._gui.search_nationality.appendChild(new Option(tmp.nationality));
  }
  var schools = (this._message['school']) ? this._message['school'].data : [];
  this._gui.search_school.appendChild(new Option(''));
  for (var i = 0, il = schools.length; i < il; i++) {
    var tmp = schools[i];
    this._gui.search_school.appendChild(new Option(tmp.school_name));
  }
  var staffs = (this._message['staff']) ? this._message['staff'].data : [];
  this._gui.search_staff.appendChild(new Option(''));
  for (var i = 0, il = staffs.length; i < il; i++) {
    var tmp = staffs[i];
    this._gui.search_staff.appendChild(new Option(tmp.staff));
  }
  var agents = (this._message['agent']) ? this._message['agent'].data : [];
  this._gui.search_agent.appendChild(new Option(''));
  for (var i = 0, il = agents.length; i < il; i++) {
    var tmp = agents[i];
    this._gui.search_agent.appendChild(new Option(tmp.agent));
  }
  var visa_types = (this._message['visa_type']) ? this._message['visa_type'].data : [];
  this._gui.search_visa_type.appendChild(new Option(''));
  for (var i = 0, il = visa_types.length; i < il; i++) {
    var tmp = visa_types[i];
    this._gui.search_visa_type.appendChild(new Option(tmp.visa_appl_type));
  }
  //Object
  //Display
  this._gui.search_client_name.value = this._search.search_client_name;
  this._gui.search_given_name.value = this._search.search_given_name;
  for (var i = 0, il = this._gui.search_nationality.options.length; i < il; i++) {
    var tmp = this._gui.search_nationality.options[i];
    if (tmp.text == this._search.search_nationality) {
      this._gui.search_nationality.selectedIndex = i;
    }
  }
  this._gui.search_phone.value = this._search.search_phone;
  for (var i = 0, il = this._gui.search_school.options.length; i < il; i++) {
    var tmp = this._gui.search_school.options[i];
    if (tmp.text == this._search.search_school) {
      this._gui.search_school.selectedIndex = i;
    }
  }
  for (var i = 0, il = this._gui.search_staff.options.length; i < il; i++) {
    var tmp = this._gui.search_staff.options[i];
    if (tmp.text == this._search.search_staff) {
      this._gui.search_staff.selectedIndex = i;
    }
  }
  for (var i = 0, il = this._gui.search_agent.options.length; i < il; i++) {
    var tmp = this._gui.search_agent.options[i];
    if (tmp.text == this._search.search_agent) {
      this._gui.search_agent.selectedIndex = i;
    }
  }
  
  this._gui.search_visa_client_num.value = this._search.search_visa_client_num;
  this._gui.search_visa_app_num.value = this._search.search_visa_app_num;
  for (var i = 0, il = this._gui.search_visa_type.options.length; i < il; i++) {
    var tmp = this._gui.search_visa_type.options[i];
    if (tmp.text == this._search.search_visa_type) {
      this._gui.search_visa_type.selectedIndex = i;
    }
  }
  this._gui.search_submit_from.value = (this._search.search_submit_from == 0) ? '' : SimpleDate.format(SimpleDate.fromStamp(this._search.search_submit_from, false), false);
  this._gui.search_submit_to.value = (this._search.search_submit_to == 0) ? '' : SimpleDate.format(SimpleDate.fromStamp(this._search.search_submit_to, false), false);
  
  this._gui.search_pending.checked = (this._search.search_pending == 1);
  this._gui.search_issued.checked = (this._search.search_issued == 1);
  this._gui.search_declined.checked = (this._search.search_declined == 1);
  
  var _self = this;
  
  if (this._message[0].total > this._message[0].size) {
    new ModulePagination(document, this._gui.page, 500, 50, this._user, this._date, { page: this._message[0].page, total: this._message[0].total, limit: this._message[0].size, callbackFunc: function(page) { _self._page = page; _self._searchData.call(_self, null); }, context: this});
  }
  
  var tr, td, a;
  for (var i = 0, il = this._message[0].data.length; i < il; i++) {
    var tmp = this._message[0].data[i];
    
    tr = this._gui.result.insertRow(-1);
    tr.style.backgroundColor = (tr.rowIndex % 2 == 0) ? '#ececec' : '#fff';
    
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    a = document.createElement('a');
    a.href = '?t=client&i=' + tmp.client_id;
    a.appendChild(document.createTextNode((tmp.name_first == '' && tmp.name_family == '') ? '-' : tmp.name_first + ' ' + tmp.name_family));
    td.appendChild(a);
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.pass_num == '') ? '-' : tmp.pass_num));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.date_birth == 0 || tmp.date_birth == null) ? '-' : SimpleDate.format(SimpleDate.fromStamp(tmp.date_birth, false), false)));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.client_num == '') ? '-' : tmp.client_num));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.visa_appl_num == '') ? '-' : tmp.visa_appl_num));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.visa_appl_type == '') ? '-' : tmp.visa_appl_type));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.visa_appl_date_submit == 0 || tmp.visa_appl_date_submit == null) ? '-' : SimpleDate.format(SimpleDate.fromStamp(tmp.visa_appl_date_submit, false), false)));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.visa_result == '') ? 'Pending' : tmp.visa_result));
    td = tr.insertCell(-1);
    td.style.borderRight = 'solid 1px #bbb';
    td.appendChild(document.createTextNode((tmp.staff == '') ? '-' : tmp.staff));
    td = tr.insertCell(-1);
    var edit, remv;
    edit = document.createElement('img');
    edit.style.padding = "0 0 2px 0";
    edit.style.cursor = 'pointer';
    edit.src = 'image/edit.png';
    edit._visa = tmp;
    edit.onclick = function() { var p = new ModulePopupBox();
                                var div = document.createElement('div');
                                var pos = DOMUtils.findPos(this);
                                p._open(div, { pos: [pos[0] - 900, pos[1] - 400], width: 1200, t: 'Visa' });
                                div.style.verticalAlign = 'top';
                                new ModuleVisaPlus(document, div, null, null, _self._user, _self._date, { mode: Constant.VPLUS_MODE_VISA, object: this._visa, popupBox: p, callbackFunc: function() { _self._searchData.call(_self); } });
                              };
    remv = document.createElement('img');
    remv.style.padding = "0 0 4px 10px";
    remv.style.cursor = 'pointer';
    remv.src = 'image/delete.png';
    remv._visa = tmp;
    remv.onclick = function() { new RequestUtils()._custom('visa', JSON.stringify([{ n: 'pz_visa', a: [], r: [this._visa] }]), function(result, params) { _self._searchData.call(_self); }, { pos: DOMUtils.findPos(this) }); };
    
    td.appendChild(edit);
    td.appendChild(remv);
  }
  
  if (this._message[0].data.length == 0) {
    tr = this._gui.result.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 11;
    td.style.padding = '10px 0 10px 30px';
    td.style.color = '#ff0000';
    td.style.fontWeight = 'bold';
    td.appendChild(document.createTextNode('No Records Found'));
  }
  //Event
  this._gui.search_client_name.onchange = function() { _self._search.search_client_name = this.value; };
  this._gui.search_given_name.onchange = function() { _self._search.search_given_name = this.value; };
  this._gui.search_nationality.onchange = function() { _self._search.search_nationality = this.options[this.selectedIndex].text; };
  this._gui.search_phone.onchange = function() { _self._search.search_phone = this.value; };
  this._gui.search_school.onchange = function() { _self._search.search_school = this.options[this.selectedIndex].text; };
  this._gui.search_staff.onchange = function() { _self._search.search_staff = this.options[this.selectedIndex].text; };
  this._gui.search_agent.onchange = function() { _self._search.search_agent = this.options[this.selectedIndex].text; };
  
  this._gui.search_visa_client_num.onchange = function() { _self._search.search_visa_client_num = this.value; };
  this._gui.search_visa_app_num.onchange = function() { _self._search.search_visa_app_num = this.value; };
  this._gui.search_visa_type.onchange = function() { _self._search.search_visa_type = this.options[this.selectedIndex].text; };
  this._gui.search_submit_from.onclick = function() { _self._selectDate.call(_self, this, _self._search.search_submit_from, function(sd) { _self._search.search_submit_from = SimpleDate.toStamp(sd); }, false); };
  this._gui.search_submit_to.onclick = function() { _self._selectDate.call(_self, this, _self._search.search_submit_to, function(sd) { _self._search.search_submit_to = SimpleDate.toStamp(sd); }, false); };

  this._gui.search_pending.onchange = function() { _self._search.search_pending = (this.checked) ? 1 : 0; };
  this._gui.search_issued.onchange = function() { _self._search.search_issued = (this.checked) ? 1 : 0; };
  this._gui.search_declined.onchange = function() { _self._search.search_declined = (this.checked) ? 1 : 0; };
  
  //Submit
  this._gui.search.onclick = function() { _self._page = 1;
                                          _self._searchData.call(_self, DOMUtils.findPos(this));
                                        };
};

HandleVisas.prototype._selectDate = function(display, date, callbackFunc, showTime) {
  var div = document.createElement('div');
  var ds = new DateSelect((date == 0) ? null : SimpleDate.fromStamp(date, false), null, { container: div, showTime: showTime });
  ds._selectFunc = function(sd) { callbackFunc(sd);
                                  if (sd) { 
                                    display.value = SimpleDate.format(sd, showTime);
                                  } else {
                                    display.value = '';
                                  }
                                  popupBox._close();
                                };
  
  var popupBox = new ModulePopupBoxSimple();
  var pos = DOMUtils.findPos(display);
  popupBox._open(div, { pos: [pos[0], pos[1] + 85] });
};

HandleVisas.prototype._searchData = function(pos) {
  var _self = this;
  new RequestUtils()._custom('visas',
                             JSON.stringify({ s: _self._search, p: _self._page, o: _self._order, q: _self._queue }),
                             function(result, params) { _self._message[0] = result;
                                                        _self._createElements.call(_self);
                                                      }, { pos: pos });
};
